#include "curve.h"
#include <QDebug>

Curve::Curve()
{
    minX = 0;
    maxX = 0;
    minY = 0;
    maxY = 0;
    baseID = 0;
}

void Curve::setAxis(QVector<QString> x)
{
    axisX = x.at(0);
    axisY = x.at(1);
}

int Curve::addCurve(QPair<float *, float *> c, int size)
{
    baseID++;
    curves.insert(baseID,c);
    sizes.insert(baseID, size);
    return baseID;
}

bool Curve::removeCurve(uint curveId)
{
    if(!curves.contains(curveId))
        return false;
    curves.take(curveId);
    sizes.take(curveId);
    return true;
}

int Curve::Draw(mglGraph *gr)
{
    gr->LoadFont("termes");
    gr->SetRanges(0, 3, 0, 16);
    gr->SetFontSize(3);
    gr->SetTicks('x', 1);
    gr->Box();
    gr->Label('x', axisX.toAscii());
    gr->Label('y', axisY.toAscii());
    int i  = 0;
    if(curves.keys().size() == 0)
    {
        gr->Axis();
        return 0;
    }
    for(uint key = 2; key>0;key--)
    {
        int size = sizes.value(key);
        QPair<float*, float*> c  = curves.value(key);
        for(int i = 0; i< size; i++)
        {
            float x = c.first[i];
            float y = c.second[i];
            if(maxX < x) maxX = x;
            if(maxY < y) maxY = y;
            if(minX > x) minX = x;
            if(minY > y) minY = y;
        }

        mglData x ,y;
        x.Set(c.first, size);
        y.Set(c.second, size);
        char color[3];

            color[0] = curveColors[i%sizeof(curveColors)];
            color[1] = '\0';

        gr->Plot(x,y,color);
        i++;
    }
    gr->Axis();
    return 0;
}
